package com.mairuide.phone.treeview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mairuide._frame.utils.AccountNode;
import com.mairuide.bill.treeview.BillTreeNode;

@Service
@Transactional(readOnly=true)
public class PhoneTreeService {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 获取案件下面的话单手机号码
	 * @param caseId
	 * @return
	 */
	public List<AccountNode> getPhoneTree(String caseId){
		List<AccountNode> nodes = new ArrayList<AccountNode>();//把所有资源转换成树模型的节点集合，此容器用于保存所有节点
		AccountNode rootNode = new AccountNode();
		rootNode.setNodeId("0");
		rootNode.setText("案件人员信息");
		nodes.add(rootNode);
		
		StringBuffer sb = new StringBuffer();
		sb.append("select distinct service_number from tb_phone_recording where is_delete=0 ");
		if(StringUtils.isNotEmpty(caseId)) {
			sb.append(" and casemanage_id ='"+caseId+"' ");
		}
		sb.append(" order by service_number asc");
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sb.toString());
		if(list!=null && list.size()>0) {
			List<AccountNode> nodes2 = new ArrayList<AccountNode>();//把所有资源转换成树模型的节点集合，此容器用于保存所有节点
			for(Map<String, Object> m:list) {
				if(m.get("SERVICE_NUMBER")!=null) {
					String phone = m.get("SERVICE_NUMBER").toString();
					AccountNode node = new AccountNode();
					node.setNodeId(phone);
					node.setText(phone);
					node.getState().setExpanded(true);
					node.setSelectable(true);
					node.getState().setSelecttable(true);
					nodes2.add(node);
				}
			}
			rootNode.setNodes(nodes2);
		}
		return nodes;
	}
	
	/**
	 * 获取案件下面的人员手机号等信息
	 * @param caseId
	 * @return
	 */
	public List<BillTreeNode> getPhoneTree2(String caseId){
		List<BillTreeNode> nodes = new ArrayList<BillTreeNode>();//把所有资源转换成树模型的节点集合，此容器用于保存所有节点
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT MAX(T.SERVICE_USER_INFORMATION) AS SERVICE_USER_INFORMATION, T.SERVICE_NUMBER AS SERVICE_NUMBER FROM TB_PHONE_RECORDING T where IS_DELETE = 0 ");
		if(StringUtils.isNotEmpty(caseId)) {
			sb.append(" and T.casemanage_id ='" + caseId + "' and T.SERVICE_NUMBER IS NOT NULL ");
		}
		sb.append(" GROUP BY SERVICE_NUMBER ORDER BY SERVICE_NUMBER ASC");
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sb.toString());
		if(list!=null && list.size()>0) {
			Map<String, List<String>> phoneMap = new HashMap<String, List<String>>();
			int unknow=1;
			for(Map<String, Object> phone : list) {
				String serviceUserInformation = phone.get("SERVICE_USER_INFORMATION")==null ? "" : phone.get("SERVICE_USER_INFORMATION").toString();
				String serviceNumber = phone.get("SERVICE_NUMBER")==null ? "" : phone.get("SERVICE_NUMBER").toString();
				
				List<String> serviceNumbers = new ArrayList<String>();
				if(StringUtils.isEmpty(serviceUserInformation)) {
					if(serviceNumbers.contains(serviceNumber)) {
						continue;
					}
					serviceUserInformation = "未知" + unknow;
					unknow++;
				}
				if(phoneMap.get(serviceUserInformation)!=null) {
					serviceNumbers = phoneMap.get(serviceUserInformation);
				}
				if(StringUtils.isNotBlank(serviceNumber)) serviceNumbers.add(serviceNumber);
				
				phoneMap.put(serviceUserInformation, serviceNumbers);
			}
			int i=1;
			for (String serviceUserInformation : phoneMap.keySet()) {
				BillTreeNode treeNode = new BillTreeNode();
				treeNode.setId(i+"");
				treeNode.setName(serviceUserInformation);
				treeNode.setTitle(serviceUserInformation);
				treeNode.setPId("pid_0");
				treeNode.setOpen(false);
				treeNode.setIsParent(true);
	        	nodes.add(treeNode);
				
				List<String> serviceUserInformations = phoneMap.get(serviceUserInformation);
				if(serviceUserInformations!=null && serviceUserInformations.size()>0) {
					for(int j=0;j<serviceUserInformations.size();j++) {
						int n = j+1;
						String accountBodyCard = serviceUserInformations.get(j).toString();
						BillTreeNode treeNode2 = new BillTreeNode();
						treeNode2.setId(i+"_"+n);
						treeNode2.setName(accountBodyCard);
						treeNode2.setAccountBody(serviceUserInformation);
						treeNode2.setAccountBodyCard(accountBodyCard);
						treeNode2.setTitle(accountBodyCard);
						treeNode2.setPId(i+"");
						treeNode2.setOpen(false);
						treeNode2.setIsParent(false);
			        	nodes.add(treeNode2);
					}
				}
				i++;
			}
		}
		return nodes;
	}
}
